<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    
    <title>问卷调查标题管理</title>
    
	<link rel="stylesheet" type="text/css" href="<%=path%>/JS/extjs/resources/css/ext-all.css"/>
	<script type="text/javascript" src="<%=path%>/JS/extjs/adapter/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/JS/extjs/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/JS/extjs/ext-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=path%>/JS/jquery-1.7.2.js"></script>
	
  </head>
  
  <body>
    <script type="text/javascript">

    //选中行的ID
	var qTitleId = '';
	var data = "";
	var win = null;
	var store = null;
	//展会store
	var showInfoStore = null;
	var type;
	var grid = null;//列表
	
	//刷新列表
	function refresh(store){
		store.reload();
		grid.getSelectionModel().clearSelections();
		qTitleId = '';
      	data = '';
	}
	
	function saveQTitle(){
		var formPanel = Ext.getCmp("formPanel");
		var formvalue = Ext.util.JSON.encode(formPanel.getForm().getValues());
		if(formPanel.getForm().isValid()){
			formPanel.getForm().submit({
				 	url : '${pageContext.request.contextPath}/qTitleAction!saveQTitle.action',
				 	contentType: "application/x-www-form-urlencoded; charset=utf-8",
				 	params:{formStr:encodeURIComponent(formvalue)},
                    success : function(from, action) {
						Ext.Msg.alert('提示', '保存成功！',function(){
							win.close();
							refresh(store);
						});
                    },
                    failure : function(form, action) {
                    	Ext.Msg.alert('提示', '保存失败！');
                    },
                    submitEmptyText: false,
                    waitMsg : '正在保存数据，稍后...'
			});
		}else{
			Ext.Msg.alert("提示","输入有错误");
		}
	}

	function deleteTitle(selIds){
		alert(selIds);
		var loadMask = new Ext.LoadMask(Ext.getBody(),{msg:"正在删除，请稍候..."});
		//var loadMask = loadMask();
		Ext.MessageBox.confirm("提示","确定删除？",function(btn){
			if(btn=="yes"){
				if(selID == ""){
					Ext.Msg.alert("提示","请选择删除行");
				}else if(selID != ""){
					loadMask.show();
					Ext.Ajax.timeout = 3 * 60 * 1000;
	        		Ext.Ajax.request({
	        		       url: '${pageContext.request.contextPath}/qTitleAction!deleteQTitle.action',
	        		       method : 'POST',
	        		       params:{id:selIds},
	        			   success: function(response, options){
		        		   		var resultjson = Ext.util.JSON.decode(response.responseText);
	        			  	 	//alert("*********" + resultjson.data);
	        			  	 	loadMask.hide();
	            			   	Ext.Msg.alert("提示", resultjson.data);
	        			  	 	refresh(store);
	        		       },
	        			   failure : function(request){   
	        		           Ext.MessageBox.show({   
	        		                   title : '提示',   
	        		                    msg : "操作失败",   
	        		                    buttons : Ext.MessageBox.OK,   
	        		                    icon : Ext.MessageBox.ERROR   
	        		                    });   
	        		       }
	        		  });
				}
			}else if(btn=="no" || btn=="cancel"){
				//return false;
			}
		});
		
	}
	

	function openDialog(){
		Ext.useShims=true;

	    win=new Ext.Window(
	    	    {
		    	    //title:"新增",
	        	    width:370,
	        	    height:200,
	        	    modal:true,
	        	    closeAction:"close",
	        	    layout:"fit",
	        	    items:[
						new Ext.FormPanel({
							id:"formPanel",
								frame:true,
								x:40,
						        y:30,
								labelAlign:"right",
								labelWidth: 85,
						        width:360,
						        height:400,
						        border:false,
						        waitMsgTarget: true,
						      //  region:"west",
						        reader : new Ext.data.JsonReader({
						        	//root:'showInfoList'
							        }, [
						            {name:'id',mapping:'id',type:'int'}, 
						            {name:'show_id',mapping:'show_id'},
						            {name:'Qtitle',mapping:'Qtitle'},
						            {name:'isCheckBox',mapping:'isCheckBox'}, 
						            {name:'isAvalible',mapping:'isAvalible',type:'int'}
						            ]),
						        items: [
						            new Ext.form.FieldSet({
						               	title: '问卷调查标题',
						               	width:340,
						               	autoHeight: true,
						                defaultType: 'textfield',
						                items: [{
						                        fieldLabel: 'ID',
						                        //emptyText: '如stam.qq.com',
						                        name: 'id',
						                      //  allowBlank: false,
						                        width:190,
						                        hidden:true
						                    },
						                	new Ext.form.ComboBox({
						    		   			   hiddenName:'show_id',
						    		               fieldLabel: '展会名称',
						    		               editable : false,
						    		               store: showInfoStore,
						    		               valueField:'id',
						    		               displayField:'showName',
						    		               typeAhead: true,
						    		               mode: 'local',
						    		               triggerAction: 'all',
						    		               emptyText:'请选择展会',
						    		               selectOnFocus:true,
						    		               width:190 
						    		    	   	}),
						    		    	{
						                        fieldLabel: '标题名',
						                        //emptyText:'如123456@qq.com',
						                        name: 'Qtitle',
						                        allowBlank: false,
						                        width:190
						                    },
						                    new Ext.form.ComboBox({
						                        fieldLabel: '选择模式',
						                        hiddenName:'isCheckBox',
						                        allowBlank:false,
						                        //blankText:'不能为空',
						                        store: new Ext.data.ArrayStore({
						                            fields: ['abbr', 'isCheckBox'],
						                            data : [
								                            [0,'单选'],
								                            [1,'多选']] 
						                        }),
						                        valueField:'abbr',
						                        displayField:'isCheckBox',
						                        typeAhead: true,
						                        mode: 'local',
						                        triggerAction: 'all',
						                        selectOnFocus:true,
						                        width:190
						                    })
						                    ,
						                    new Ext.form.ComboBox({
						                    fieldLabel: '是否可用',
						                    hiddenName:'isAvalible',
						                    allowBlank:false,
						                    blankText:'不能为空',
						                    store: new Ext.data.ArrayStore({
						                        fields: ['abbr', 'isAvalible'],
						                        data : [
							                            [0,'否'],
							                            [1,'是']] 
						                    }),
						                    valueField:'abbr',
						                    displayField:'isAvalible',
						                    typeAhead: true,
						                    mode: 'local',
						                    triggerAction: 'all',
						                    selectOnFocus:true,
						                    width:190
						                })
						                ]
						            })
						        ]
						})
	   	        	    ],
	        	    buttons:[{
	        	    	text:"确认",handler:function(){
	        	    	saveQTitle();
	        	    	
	        	    }}, {
		        	    text:"取消",handler:function(){
	        	    	win.close();
	        	    	}
			       	}]
	        	}); 
	    win.show();
		//点击右上角的关闭按钮后
	    win.on("close",function(){
		});
	}


  		function InitMain(){
  			 var url = document.location.href;
 			var arrStr = url.substring(url.indexOf("?")+1).split("&");
 			type=arrStr[0].replace("type=","");
 			
 			//复选框
			var sm = new Ext.grid.CheckboxSelectionModel({checkOnly:false});

  			var cm = new Ext.grid.ColumnModel([
  			                              sm,
   	  	                 	  	          {
   	  	              	  	                  id       :'id',
   	  	              	  	                  header   : 'ID', 
   	  	              	  	                  width    : 50, 
   	  	              	  	                  sortable : true, //排序
   	  	              	  	              	 // hidden : true,//隐藏列
   	  	              	  	                  dataIndex: 'id'
   	  	              	  	              },
   	  	              	  	              {
   	  	              	  	                  header   : '标题名', 
   	  	              	  	                  width    : 75, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                 // renderer : 'usMoney', 
   	  	              	  	                  dataIndex: 'Qtitle'
   	  	              	  	              },
   	  	              	  	              {
   	  	              	  	                  header   : '问题选择类型', 
   	  	              	  	                  width    : 60, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  renderer : showSelType, 
   	  	              	  	                  dataIndex: 'isCheckBox'
   	  	              	  	              },{
   	  	              	  	                  header   : '是否可用', 
   	  	              	  	                  width    : 40, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  renderer : isVailabel, 
   	  	              	  	                  dataIndex: 'isAvalible'
   	  	              	  	              },{
   	  	              	  	                  header   : 'showId', 
   	  	              	  	                  width    : 250, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  hidden   :true,
   	  	              	  	                  dataIndex: 'show_id'
   	  	              	  	              },{
   	  	              	  	                  header   : '展会名称', 
   	  	              	  	                  width    : 250, 
   	  	              	  	                  sortable : true, 
   	  	              	  	                  dataIndex: 'showName'
   	  	              	  	              }
   	  	              	  	          ]);

    	 // 	var proxy = new Ext.data.MemoryProxy(audienceArr);
    	 	var proxy = new Ext.data.HttpProxy({
        	 	url:'${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action',
        	 	method:'post'
        	})
    	  	var record = Ext.data.Record.create([
                                        {},
    	  	               				{name: 'id', type: 'int'},
    	  	          					{name: 'Qtitle'},
    	  	          					{name: 'isCheckBox'},
    	  	          					{name: 'isAvalible'},
    	  	          					{name: 'show_id'},
    	  	          					{name: 'showName'}
    	  	            	  	          ]);
  	          var reader = new Ext.data.JsonReader({
  	  	          root:'qTitleList'
  	  	          },record);


  	  	      store = new Ext.data.Store({
  	  	          proxy:proxy,
  	  	          reader:reader
  	  	      });
  	  	  	  store.load({});

  	  	      //显示性别
  	  	      function showSelType(val){//0为男，1为女
  	  	  	      if(val == 0){
  	  	  	  	      return '单选';
  	  	  	      }else if(val == 1){
  	  	  	  	      return '多选';
  	  	  	      }
  	  	  	      return val;
  	  	      }
			  //用户是否可用
	  	  	  function isVailabel(val) {
	  	        if (val == 0) {
	  	            return '否';
	  	        }else if (val == 1) {
	  	            return '是';
	  	        }
	  	        return val;
	  	      }

  	  	      // create the Grid
  	  	      grid = new Ext.grid.GridPanel({
  	  	          store: store,
  	  	     	  cm: cm,
  	  	          stripeRows: true,
  	  	          autoExpandColumn: 'id',
  	  	          width: "100%",
	  	  	      //sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
	  	  	      sm: sm,
	  			  viewConfig: {
	  				  forceFit: true
	  			  },
  	  	          stateful: true,
  	  	          stateId: 'grid',
  	  	          region:"north"
  	  	      });

		  //点击选中行，修改
  	  	  grid.getSelectionModel().on('rowselect', function(sm, rowIdx, r){
            	qTitleId = r.data.id;
            	data = r.data;
        	});
  	  	showInfoStore = new Ext.data.Store({
			  proxy:new Ext.data.HttpProxy({url:'${pageContext.request.contextPath}/showInfoManagerAction!getAllShowInfo.action'}),
			  reader:new Ext.data.JsonReader({root:'showInfoList'},['id','showName'])  //显示的字段名，与服务器端返回的json中字段一致
			  });
	  	showInfoStore.load(); 

		//机构类型 表单
		var qTitleForm = new Ext.FormPanel({
			frame:true,
			labelAlign:"right",
	        width:340,
	        border:false,
	        waitMsgTarget: true,
	        region:"north",
	        layout:"form",
			items:[
		    	   	
		    	   	new Ext.form.ComboBox({
		   			   id:'showInfoCombox',
		   			   name:'showInfo',
		               fieldLabel: '展会名称',
		               hiddenName:'state',
		               store: showInfoStore,
		               valueField:'id',
		               displayField:'showName',
		               typeAhead: true,
		               mode: 'local',
		               triggerAction: 'all',
		               emptyText:'请选择展会',
		               selectOnFocus:false,
		               width:240 
		    	   	})
			       ]
			});
		//根据展会查询调查问郑标题
		Ext.getCmp("showInfoCombox").on('select',function(combo,record,index){
			//当前选择的机构类的id
			var showId = Ext.getCmp('showInfoCombox').getValue();
			var qTitleName = Ext.getCmp('showInfoCombox').getRawValue();

			//根据选择的标题重新加载grid
  	       var newUrl = '${pageContext.request.contextPath}/qTitleAction!getAllQTitle.action?showId='+showId;
  	       store.proxy.setUrl(newUrl);
  		   store.load();
  		   //重新加载数据后，需重新选择行
  		 	grid.getSelectionModel().clearSelections();
 			qTitleId = '';
       		data = '';

		});
		
		var northPanel = new Ext.Panel({
			id:"northPanel",
			region:"north",
			//split:"true",
			layout:"fit",
			height:70,
			items:[
				qTitleForm
			],
			tbar:[//定义顶部工具栏
 				 {
         	      id:'addnode',
                   text: '新增',
                   iconCls:'add',
                   //iconCls : 'edit',
                   //pressed : true,
                   handler : function(){
 				   		openDialog();
 				   		win.setTitle("新增");
                   }
                  },'-',{
         	      id:'updatenode',
                   text: '修改',
                   iconCls : 'option',
                   //pressed : true,
                   handler : function(){
                	  if(qTitleId == ''){
	                	  Ext.Msg.alert("提示","请选择修改行");
                	  }else{
	                   openDialog();
	                   win.setTitle("修改");
	                   Ext.getCmp("formPanel").form.loadRecord(new Ext.data.MemoryProxy(data));
                	  }
	                   }
                  },'-',{
         	      id:'deletenode',
                   text: '删除',
                   iconCls : 'remove',
                   //pressed : true,
                   handler : function(){
                	   var obArr = sm.getSelections();
                   	   var selIDStr = "";
                   	   if(obArr.length == 0){
	                   	   Ext.Msg.alert("提示","没有选中行");
	                   	   return;
                   	   }
                   	   for(var i=0;i<obArr.length;i++){
	                   	   if(selIDStr > 0){
	                   			selIDStr += ",";
	                   	   }
                   			selIDStr += obArr[i].id;
                   	   }
                	   
                	  deleteTitle(selIDStr);
	                  }
                  }]
			});
  	  	      
  	  	  var centerPanel = new Ext.Panel({
	  	  		id:"centerPanel",
	  	  		layout:"fit",
	  	  		region:"center",
	  	  		//width:"75%",
	  	  		items:[
						grid
	   	  	  		]
	  	  		
	  		});
  	  	var viewport = new Ext.Viewport({
			id : 'viewprot',
			layout : 'border',
			items : [
					northPanel,
					centerPanel
				]
		});
  	}
  		Ext.onReady(InitMain); 
  	
  	
  	</script>
  </body>
</html>
